// 用户管理模块
var express = require('express');
var router = express.Router();
var jwt = require('jsonwebtoken');
// 导入db ,操作数据库

const { getAll, getOne, exec } = require('../db/index');
const { debug } = require('../config');
const secret = 'register-rule';
/**
 * 分页用户
 * GET /users
 */
router.get('/', async (req, res) => {
  // console.log(req);
  // console.log(req.query)
  // 解析请求参数
  // 解构query对象,提取page,size
  // 如果没有值,就给默认值1,4
  // const { page = 1, size = 4, user_name } = req.query
  const { page = 1, size = 4 } = req.query;
  // 获取总数

  let sql = `select count(*) as total  from user`;
  const obj = await getOne(sql); // { total: 10 }
  console.log(obj);
  // 根据分页公式, 计算offset偏移量
  let offset = (page - 1) * size;
  // 操作数据库--只显示状态为1的用户 where user_status = "1"
  sql = `select * from user order by user_id asc  limit ${offset}, ${size} `;
  try {
    const data = await getAll(sql);

    res.send({
      code: 0,
      message: '获取用户成功',
      result: {
        total: obj.total,
        data: data,
      },
    });
  } catch (err) {
    res.send({
      code: 'err',
      message: '获取所有用户失败',
      result: debug ? '' : err,
    });
  }
});

/**
 * 新增用户 POST /user
 */

router.post('/', async (req, res) => {
  const { user_name, user_pwd, user_type } = req.body;

  let sql = `insert into user (user_name,user_pwd, user_type)  values  ('${user_name}', '${user_pwd}','${user_type}')`;
  try {
    const data = await exec(sql);
    console.log(data);
    res.send({
      code: 0,
      message: '新增用户成功',
      result: {
        user_id: data.insertId,
        user_name,
        user_pwd,
        user_type,
      },
    });
  } catch (err) {
    res.send({
      code: 'err',
      message: '新增用户失败',
      result: debug ? '' : err,
    });
  }
});

// 根据id获取
router.get('/id', async (req, res) => {
  console.log(req.query);
  const { id } = req.query;
  let sql = `select * from user where user_id = ${id}`;
  try {
    const data = await getOne(sql);
    res.send({
      code: 0,
      message: '根据ID获取用户成功',
      result: {
        data,
      },
    });
  } catch (err) {
    res.send({
      code: 'err',
      message: '根据ID获取用户失败',
      result: debug ? '' : err,
    });
  }
});

// 根据用户名查用户并分页
router.post('/username', async (req, res) => {
  const { page = 1, size = 4, user_name } = req.body;
  console.log(req.body);
  // 获取总数
  let sql = `select count(*) as total  from user  where user_name like'%${user_name}%'`;
  const obj = await getOne(sql);
  console.log(obj);
  let offset = (page - 1) * size;
  // 操作数据库
  sql = `select * from user where user_name like'%${user_name}%' order by user_id asc  limit ${offset}, ${size} `;
  try {
    const data = await getAll(sql);
    res.send({
      code: 0,
      message: '获取用户成功',
      result: {
        total: obj.total,
        data: data,
      },
    });
  } catch (err) {
    res.send({
      code: 'err',
      message: '获取所有用户失败',
      result: debug ? '' : err,
    });
  }
});
// 修改账户/密码/类型/状态
router.put('/id', async (req, res) => {
  const { id } = req.query;
  const { user_name, user_pwd, user_status, user_type } = req.body;
  let sql = `update user set user_name='${user_name}',user_pwd='${user_pwd}',user_status='${user_status}',user_type='${user_type}'where user_id=${id} `;
  try {
    const data = await exec(sql);
    res.send({
      code: 0,
      message: '修改用户成功',
      result: {
        user_id: id,
        user_name,
        user_pwd,
        user_status,
        user_type,
      },
    });
  } catch (err) {
    res.send({
      code: 'err',
      message: '修改用户失败',
      result: debug ? '' : err,
    });
  }
});

//修改用户状态--启用与禁用
router.put('/id/status', async (req, res) => {
  console.log('hello');
  const { id } = req.query;
  const { user_status } = req.body;
  let sql = `update user set user_status='${user_status}'where user_id=${id} `;
  try {
    const data = await exec(sql);
    res.send({
      code: 0,
      message: '改变用户状态成功',
      result: {
        user_id: id,

        user_status,
      },
    });
  } catch (err) {
    res.send({
      code: 'err',
      message: '改变用户状态失败',
      result: debug ? '' : err,
    });
  }
});

module.exports = router;
